package com.selfdriving.activity;

import java.util.Calendar;
import net.tsz.afinal.annotation.view.ViewInject;
import com.selfdriving.R;
import com.selfdriving.view.wheelview.ScreenInfo;
import com.selfdriving.view.wheelview.WheelMain;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class AddTravelActivity extends BaseActivity {

	@ViewInject(id = R.id.btnStartTime, click = "btnStartTime")
	Button btnStartTime;

	@ViewInject(id = R.id.btnEndTime, click = "btnEndTime")
	Button btnEndTime;

	@ViewInject(id = R.id.sort_spinner)
	Spinner sort_spinner;
	
	@ViewInject(id = R.id.age_spinner)
	Spinner age_spinner;

	private WheelMain wheelMain;

	private AlertDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View contentView = LayoutInflater.from(this.getParent()).inflate(R.layout.add_travel, null);
		setContentView(contentView);
		String[] mItems = getResources().getStringArray(R.array.travel_sort);
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
		ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.age));
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
		sort_spinner.setAdapter(adapter);
		age_spinner.setAdapter(adapter2);
	}

	public void btnEndTime(View v) {
		showDateTimePicker(btnEndTime);
	}

	public void btnStartTime(View v) {
		showDateTimePicker(btnStartTime);
	}

	public void showDateTimePicker(final Button button) {
		LayoutInflater inflater = LayoutInflater.from(this);
		View timepickerview = inflater.inflate(R.layout.selectbirthday, null);
		timepickerview.setMinimumWidth(getWindowManager().getDefaultDisplay()
				.getWidth());
		ScreenInfo screenInfo = new ScreenInfo(this);
		wheelMain = new WheelMain(timepickerview);
		wheelMain.screenheight = screenInfo.getHeight();
		Calendar calendar = Calendar.getInstance();
		// calendar.setTime(dateFormat.parse(time));设置指定时间
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		wheelMain.setTime(year, month, day);
		dialog = new AlertDialog.Builder(getParent()).setView(timepickerview)
				.show();

		Window window = dialog.getWindow();
		window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
		window.setWindowAnimations(R.style.mystyle); // 添加动画

		Button btn = (Button) timepickerview
				.findViewById(R.id.btn_datetime_sure);
		btn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				button.setText(wheelMain.getTime());
				dialog.dismiss();
			}
		});

	}

}
